define([
    'jquery',
    'underscore',
    'backbone',
    'text!login/login.html',
    'js/configAmd',
    'js/myUtilsAmd',

], function ($, _, Backbone, tpl, config, myUtils) {
    console.log('this is login view');

    var View = Backbone.View.extend({
        render: function () {
            console.log('render');
            var tmpl = _.template(tpl);
            $(this.el).html(tmpl);
            return this;
        },

        events: {
            "click #doLogin": "doLogin"
        },

        doLogin: function (event) {
            var formData = $('form#login').serializeObject();
            console.log(formData);
            var self = this;

            var nodeTip = $(self.el).find('#tips');
            nodeTip.text();

            $.ajax({
                url: config.getHost() + "/admin/login",
                type: "POST",
                headers: {"Content-Type": "application/json;charset=UTF-8"},
                data: JSON.stringify(formData),
                success: function (res) {
                    console.log('succ', res);
                    blueInkApp.loadModule("admin");
                },
                error: function (xhr) {
                    console.log('error', xhr.status, xhr.responseText);
                    nodeTip.text(xhr.responseText);
                }
            });
        }
    });

    return View;
});